package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Room;
import com.ruoyi.system.domain.RoomOrder;
import com.ruoyi.system.domain.RoomStatus;
import com.ruoyi.system.domain.Tourist;
import com.ruoyi.system.enums.RoomOrderStatusEnum;
import com.ruoyi.system.enums.RoomStatusEnum;
import com.ruoyi.system.mapper.RoomOrderMapper;
import com.ruoyi.system.service.IRoomOrderService;
import com.ruoyi.system.service.IRoomService;
import com.ruoyi.system.service.IRoomStatusService;
import com.ruoyi.system.service.ITouristService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 客房订单Service业务层处理
 *
 * @author 强盛酒店
 * @date 2023-03-09
 */
@Service
public class RoomOrderServiceImpl implements IRoomOrderService {
    @Autowired
    private RoomOrderMapper roomOrderMapper;
    SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private IRoomStatusService roomStatusService;
    @Autowired
    private ITouristService touristService;
    @Autowired
    private IRoomService roomService;
    /**
     * 查询客房订单
     *
     * @param id 客房订单主键
     * @return 客房订单
     */
    @Override
    public RoomOrder selectRoomOrderById(Long id) {
        return roomOrderMapper.selectRoomOrderById(id);
    }

    /**
     * 查询客房订单列表
     *
     * @param roomOrder 客房订单
     * @return 客房订单
     */
    @Override
    public List<RoomOrder> selectRoomOrderList(RoomOrder roomOrder) {
        return roomOrderMapper.selectRoomOrderList(roomOrder);
    }

    /**
     * 新增客房订单
     *
     * @param roomOrder 客房订单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertRoomOrder(RoomOrder roomOrder) throws ParseException {
        roomOrder.setCreateTime(DateUtils.getNowDate());
        roomOrder.setCreateUser(SecurityUtils.getUserId());
        roomOrder.setStatus(RoomOrderStatusEnum.ARRANGED.getCode());
        int flag = roomOrderMapper.insertRoomOrder(roomOrder);
        List<String> dates = findEveryDay(dateFormat.format(roomOrder.getLiveTime()),dateFormat.format(roomOrder.getLeaveTime()));
        for (String date : dates) {
            RoomStatus roomStatus = new RoomStatus();
            roomStatus.setRoomId(roomOrder.getRoomId());
            roomStatus.setOrderDate(date);
            roomStatus.setOrderId(roomOrder.getId());
            roomStatus.setStatus(RoomStatusEnum.BEEN_IN.getCode());
            roomStatusService.insertRoomStatus(roomStatus);
        }
        return flag;
    }

    /**
     * 传入两个时间范围，返回这两个时间范围内的所有日期，并保存在一个集合中
     * @param beginTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static List<String> findEveryDay(String beginTime, String endTime)
            throws ParseException {
        //1.创建一个放所有日期的集合
        List<String> dates = new ArrayList<>();
        //2.创建时间解析对象规定解析格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //3.将传入的时间解析成Date类型,相当于格式化
        Date dBegin = sdf.parse(beginTime);
        Date dEnd = sdf.parse(endTime);
        //4.将格式化后的第一天添加进集合
        dates.add(sdf.format(dBegin));
        //5.使用本地的时区和区域获取日历
        Calendar calBegin = Calendar.getInstance();
        //6.传入起始时间将此日历设置为起始日历
        calBegin.setTime(dBegin);
        //8.判断结束日期是否在起始日历的日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 9.根据日历的规则:月份中的每一天，为起始日历加一天
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            //10.得到的每一天就添加进集合
            dates.add(sdf.format(calBegin.getTime()));
            //11.如果当前的起始日历超过结束日期后,就结束循环
        }
        return dates;
    }

    /**
     * 修改客房订单
     *
     * @param roomOrder 客房订单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRoomOrder(RoomOrder roomOrder) throws ParseException {
        roomOrder.setUpdateTime(DateUtils.getNowDate());
        roomOrder.setStatus(RoomOrderStatusEnum.ARRANGED.getCode());
        int flag = roomOrderMapper.updateRoomOrder(roomOrder);
        List<RoomStatus> roomStatusList = roomStatusService.selectRoomStatusList(new RoomStatus() {{
            setOrderId(roomOrder.getId());
        }});
        for (RoomStatus roomStatus : roomStatusList) {
            roomStatusService.deleteRoomStatusById(roomStatus.getId());
        }
        List<String> dates = findEveryDay(dateFormat.format(roomOrder.getLiveTime()),dateFormat.format(roomOrder.getLeaveTime()));
        for (String date : dates) {
            RoomStatus roomStatus = new RoomStatus();
            roomStatus.setRoomId(roomOrder.getRoomId());
            roomStatus.setOrderDate(date);
            roomStatus.setOrderId(roomOrder.getId());
            roomStatus.setStatus(RoomStatusEnum.BEEN_IN.getCode());
            roomStatusService.insertRoomStatus(roomStatus);
        }
        return flag;
    }

    /**
     * 批量删除客房订单
     *
     * @param ids 需要删除的客房订单主键
     * @return 结果
     */
    @Override
    public int deleteRoomOrderByIds(Long[] ids) {
        // for (Long id : ids) {
        //     RoomOrder roomOrder = roomOrderMapper.selectRoomOrderById(id);
        //     List<RoomStatus> roomStatusList = roomStatusService.selectRoomStatusList(new RoomStatus() {{
        //         setOrderId(roomOrder.getId());
        //     }});
        //     for (RoomStatus roomStatus : roomStatusList) {
        //         roomStatusService.deleteRoomStatusById(roomStatus.getId());
        //     }
        // }
        return roomOrderMapper.deleteRoomOrderByIds(ids);
    }

    /**
     * 删除客房订单信息
     *
     * @param id 客房订单主键
     * @return 结果
     */
    @Override
    public int deleteRoomOrderById(Long id) {
        return roomOrderMapper.deleteRoomOrderById(id);
    }

    @Override
    public int submitRoomOrder(RoomOrder roomOrder) throws ParseException {
        Tourist sys = new Tourist();
        sys.setPhone(SecurityUtils.getUsername());
        Tourist tourist = touristService.selectTouristList(sys).get(0);
        roomOrder.setUserName(tourist.getName());
        roomOrder.setPhone(tourist.getPhone());
        roomOrder.setIdCard(tourist.getIdCard());
        roomOrder.setCreateTime(DateUtils.getNowDate());
        roomOrder.setCreateUser(SecurityUtils.getUserId());
        roomOrder.setStatus(RoomOrderStatusEnum.ARRANGING.getCode());
        int flag = roomOrderMapper.insertRoomOrder(roomOrder);
        List<String> dates = findEveryDay(dateFormat.format(roomOrder.getLiveTime()),dateFormat.format(roomOrder.getLeaveTime()));
        for (String date : dates) {
            RoomStatus roomStatus = new RoomStatus();
            roomStatus.setRoomId(roomOrder.getRoomId());
            roomStatus.setOrderDate(date);
            roomStatus.setOrderId(roomOrder.getId());
            roomStatus.setStatus(RoomStatusEnum.APPOINTMENT.getCode());
            roomStatusService.insertRoomStatus(roomStatus);
        }
        return flag;
    }

    @Override
    public int auditRoomOrder(Long id) {
        RoomOrder roomOrder = this.selectRoomOrderById(id);
        roomOrder.setStatus(RoomOrderStatusEnum.ARRANGED.getCode());
        int i = roomOrderMapper.updateRoomOrder(roomOrder);
        List<RoomStatus> roomStatusList = roomStatusService.selectRoomStatusList(new RoomStatus() {{
            setOrderId(roomOrder.getId());
        }});
        for (RoomStatus roomStatus : roomStatusList) {
            roomStatus.setStatus(RoomStatusEnum.APPOINTMENT.getCode());
            roomStatusService.updateRoomStatus(roomStatus);
        }
        return i;
    }

    @Override
    public List<RoomOrder> getMyOrder() {
        Long userId = SecurityUtils.getUserId();
        List<RoomOrder> roomOrders = roomOrderMapper.selectRoomOrderList(new RoomOrder() {{
            setCreateUser(userId);
        }});
        for (RoomOrder roomOrder : roomOrders) {
            Room room = roomService.selectRoomById(roomOrder.getRoomId());
            roomOrder.setRoom(room);
        }
        return roomOrders;
    }

    @Override
    public int cancel(Long id) {
        RoomOrder roomOrder = roomOrderMapper.selectRoomOrderById(id);
        roomOrder.setStatus(RoomOrderStatusEnum.Cancel.getCode());
        int i = roomOrderMapper.updateRoomOrder(roomOrder);
        List<RoomStatus> roomStatusList = roomStatusService.selectRoomStatusList(new RoomStatus() {{
            setOrderId(roomOrder.getId());
        }});
        for (RoomStatus roomStatus : roomStatusList) {
            roomStatus.setStatus(RoomStatusEnum.FREE.getCode());
            roomStatusService.updateRoomStatus(roomStatus);
        }
        return i;
    }
}
